/*
 * This project provides a number of implementations for indexing and evaluating
 * <i>Continuous Top-k Queries over Text Streams</i>
 * and has been developped as part of the work described in the article:
 * 
 * Vouzoukidou, N., Amann, B., & Christophides, V. (2012, October). 
 * Processing continuous text queries featuring non-homogeneous scoring functions.
 * In Proceedings of the 21st ACM international conference on Information and 
 * knowledge management (pp. 1065-1074). ACM.
 * http://dl.acm.org/citation.cfm?id=2398404
 * 
 * This software is distributed under the GNU General Public License and as an
 * additional condition of usage, please cite the aforementioned article.
 * 
 * Copyright (C) 2012 ICS/FORTH, Greece and 
 *               2012 LIP6, France
 * 
 * Author: Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gr.forth.ics.continuoustopk.index;

import gr.forth.ics.continuoustopk.Constants;
import java.awt.Point;
import static gr.forth.ics.continuoustopk.Constants.rankingFunction;

/**
 * Constraint model: The three linear stopping conditions.
 * For more information see:
 * 
 * Vouzoukidou, N., Amann, B., & Christophides, V. (2012, October). 
 * Processing continuous text queries featuring non-homogeneous scoring functions.
 * In Proceedings of the 21st ACM international conference on Information and 
 * knowledge management (pp. 1065-1074). ACM. 
 * http://dl.acm.org/citation.cfm?id=2398404
 *
 * @author Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 */
public abstract class UpdatedQueriesConstraint {
    // to prevent errors in double values operations
    private static double delta = 0.000000001;

    public static final UpdatedQueriesConstraint before45Degrees = new UpdatedQueriesConstraint() {
        @Override
        public boolean shouldVisit(double x, double y, double cs, double ci, double max) {
            return y * rankingFunction.beta() + delta >= x - cs * rankingFunction.alpha();
        }

        @Override
        public double intersection(double y, double cs, double ci, double max) {
            throw new UnsupportedOperationException("Not supported.");
        }
    };
    public static final UpdatedQueriesConstraint moreThanAverage = new UpdatedQueriesConstraint() {
        @Override
        public boolean shouldVisit(double x, double y, double cs, double ci, double max) {
            return y * Constants.maxQuerySize * ci * rankingFunction.beta() + delta >= x - cs * rankingFunction.alpha();
        }

        @Override
        public double intersection(double y, double cs, double ci, double max) {
            return y * Constants.maxQuerySize * ci * rankingFunction.beta() + rankingFunction.alpha() * cs;
        }
    };
    public static final UpdatedQueriesConstraint byMaxPotentialScore = new UpdatedQueriesConstraint() {
        @Override
        public boolean shouldVisit(double x, double y, double cs, double ci, double max) {
            return y * (ci - max) * rankingFunction.beta() + delta >= x - (rankingFunction.alpha() * cs + rankingFunction.beta() * max);
        }

        @Override
        public double intersection(double y, double cs, double ci, double max) {
            return (y * (ci - max) + max) * rankingFunction.beta() + cs * rankingFunction.alpha();
        }
    };

    // intersection between LUB and HTA
    public static Point.Double intersectionPoint(double cs, double ci, double max) {
        double bmax = rankingFunction.beta() * max;
        double divisor = max + (Constants.maxQuerySize - 1) * ci;
        double x = (ci - max) * bmax / divisor + rankingFunction.alpha() * cs;
        double y = max / divisor;
        return new Point.Double(x, y);
    }
    
    public static UpdatedQueriesConstraint combined(final UpdatedQueriesConstraint... constraints) {
        return new UpdatedQueriesConstraint() {
            @Override
            public boolean shouldVisit(double x, double y, double cs, double ci, double max) {
                for (UpdatedQueriesConstraint constraint : constraints) {
                    if (!constraint.shouldVisit(x, y, cs, ci, max)) {
                        return false;
                    }
                }
                return true;
            }

            @Override
            public double intersection(double y, double cs, double ci, double max) {
                throw new UnsupportedOperationException();
            }
        };
    }

    private UpdatedQueriesConstraint() {}

    public abstract double intersection(double y, double cs, double ci, double max);

    public abstract boolean shouldVisit(double x, double y, double cs, double ci, double max);
}
